package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor1 extends Thread {

    private Socket socket;
    private Map<String,Application> applicationMap;

    public RequestProcessor1(Socket socket, Map<String, Application> applicationMap) {
        this.socket = socket;
        this.applicationMap = applicationMap;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            Application application = applicationMap.get(request.getApplicationName());
            if(application!=null){
                if(application.getServletMap().get(request.getUrl()) !=null){
                    HttpServlet httpServlet = application.getServletMap().get(request.getUrl());
                    httpServlet.service(request,response);
                }else {
                    // 静态资源处理
                    response.outputHtml(application.getApplicationPath()+request.getUrl());
                }
            }else {
                // 静态资源处理
                response.outputHtml(request.getUrl());
            }
            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
